package cn.cxion.chat.app.activity;

import android.Manifest;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.os.Build;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.PopupWindow;

import androidx.annotation.NonNull;

import com.afollestad.materialdialogs.DialogAction;
import com.afollestad.materialdialogs.MaterialDialog;
import com.blankj.utilcode.util.FileUtils;
import com.blankj.utilcode.util.PathUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.lqr.optionitemview.OptionItemView;
import com.lxj.xpopup.XPopup;

import java.io.File;
import java.io.IOException;
import java.util.Timer;
import java.util.TimerTask;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.cxion.chat.R;
import cn.cxion.chat.app.third.location.data.LocationData;
import cn.cxion.chat.app.third.location.ui.activity.MyLocationActivity;
import cn.cxion.chat.app.util.LogUtil;
import cn.cxion.chat.app.view.popupview.DatePickPopupView;
import cn.cxion.chat.app.view.popupview.AppointmentTimePopupView;
import cn.cxion.chat.kit.WfcBaseActivity;
import cn.cxion.chat.kit.user.ChangeMyNameActivity;
import cn.wildfirechat.message.LocationMessageContent;

import static com.lxj.xpopup.enums.PopupAnimation.TranslateFromBottom;

public class DialogActivity extends WfcBaseActivity {
    @Bind(R.id.d1)
    OptionItemView d1;
    @Bind(R.id.d2)
    OptionItemView d2;
    @Bind(R.id.d3)
    OptionItemView d3;
    @Bind(R.id.d4)
    OptionItemView d4;
    @Bind(R.id.d5)
    OptionItemView d5;
    @Bind(R.id.d6)
    OptionItemView d6;
    @Bind(R.id.d7)
    OptionItemView d7;
    @Bind(R.id.d8)
    OptionItemView d8;
    @Bind(R.id.d9)
    OptionItemView d9;
    @Bind(R.id.d10)
    OptionItemView d10;

    MaterialDialog dialog2;
    public static void show(Context context){
        Intent intent=new Intent(context,DialogActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected int contentLayout() {
        return R.layout.activity_dailog;
    }

    @Override
    protected void afterViews() {
        super.afterViews();
        ButterKnife.bind(this);
    }



    @OnClick({R.id.d1, R.id.d2, R.id.d3, R.id.d4, R.id.d5, R.id.d6, R.id.d7, R.id.d8, R.id.d9, R.id.d10})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.d1:
                updateDisplayName();
                break;
            case R.id.d2:
                d2();
                break;
            case R.id.d3:
                d3();
                break;
            case R.id.d4:
                d4();
                break;
            case R.id.d5:
                d5();
                break;
            case R.id.d6:
                d6();
                break;
            case R.id.d7:
                d7();
                break;
            case R.id.d8:
                d8();
                break;
            case R.id.d9:
                break;
            case R.id.d10:
                break;
        }
    }

    private void d8() {
        PathUtils.getExternalAppFilesPath();
        LogUtil.print(PathUtils.getExternalAppFilesPath());
        File file=new File(PathUtils.getExternalAppFilesPath()+"/99.png");
        if(!file.exists()){
            try {
                file.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
            LogUtil.print("!exists");
        }else{
            LogUtil.print("exists");
        }

    }

    private void updateDisplayName() {
        MaterialDialog dialog = new MaterialDialog.Builder(this)
                .content("修改个人昵称？")
                .positiveText("修改")
                .negativeText("取消")
                .onPositive(new MaterialDialog.SingleButtonCallback() {
                    @Override
                    public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                        Intent intent = new Intent(DialogActivity.this, ChangeMyNameActivity.class);
                        startActivity(intent);
                    }
                }).build();
        dialog.show();
    }
    private  void d2(){
         dialog2 = new MaterialDialog.Builder(this)
                .content("登录中...")
                .progress(true, 100)
                .cancelable(false)
                .build();
        dialog2.show();
        Timer timer=new Timer();
        TimerTask timerTask=new TimerTask() {
            @Override
            public void run() {
                dialog2.dismiss();
            }
        };
        timer.schedule(timerTask,3000);


    }


    // 进度对话框
    private ProgressDialog mProDialog;
    private void d3(){
        mProDialog = new ProgressDialog(this);
        mProDialog.setCancelable(true);
        mProDialog.setTitle("请稍后");

        mProDialog.setOnCancelListener(new DialogInterface.OnCancelListener() {

            @Override
            public void onCancel(DialogInterface dialog) {
                // cancel进度框时,取消正在进行的操作
//                if (null != mIdVerifier) {
//                    mIdVerifier.cancel();
//                }
            }
        });
        mProDialog.setMessage("注册中...");
        mProDialog.show();
        Timer timer=new Timer();
        TimerTask timerTask=new TimerTask() {
            @Override
            public void run() {
                mProDialog.dismiss();
            }
        };
        timer.schedule(timerTask,3000);
    }

    private void d4(){
        PopupWindow popupWindow=new PopupWindow();
        popupWindow.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);
        popupWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        popupWindow.setContentView(LayoutInflater.from(this).inflate(R.layout.test_popup,null));
        popupWindow.setBackgroundDrawable(new ColorDrawable(0x00000000));
        popupWindow.setOutsideTouchable(false);
        popupWindow.setFocusable(true);
        popupWindow.showAsDropDown(d4);

    }

    private void d5(){
        new XPopup.Builder(this)
                .moveUpToKeyboard(false) //如果不加这个，评论弹窗会移动到软键盘上面
                .asCustom(new AppointmentTimePopupView(this)/*.enableDrag(false)*/)
                .show();
    }  private void d6(){
        new XPopup.Builder(this)
                .popupAnimation(TranslateFromBottom)
                .asCustom(new DatePickPopupView(this)/*.enableDrag(false)*/)
                .show();
    }
    private void d7(){
        String[] permissions = new String[]{
                Manifest.permission.ACCESS_FINE_LOCATION,
                Manifest.permission.ACCESS_COARSE_LOCATION,
                Manifest.permission.ACCESS_FINE_LOCATION
        };
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (!((WfcBaseActivity) this).checkPermission(permissions)) {
                this.requestPermissions(permissions, 100);
                return;
            }
        }
        Intent intent = new Intent(this, MyLocationActivity.class);
        startActivityForResult(intent, 100);
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == RESULT_OK) {
            LocationData locationData = (LocationData) data.getSerializableExtra("location");
//            ToastUtils.showShort(locationData+"");
            LocationMessageContent locCont = new LocationMessageContent();
            locCont.setTitle(locationData.getPoi());
            locCont.getLocation().setLatitude(locationData.getLat());
            locCont.getLocation().setLongitude(locationData.getLng());
            locCont.setThumbnail(locationData.getThumbnail());

            ToastUtils.showShort(locationData.getPoi());
        }
    }

}
